package com.ruoyi.basic.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basic.domain.Player;
import com.ruoyi.basic.service.IPlayerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 播放器配置Controller
 *
 * @author ruoyi
 * @date 2025-11-13
 */
@RestController
@RequestMapping("/basic/player")
public class PlayerController extends BaseController {
    @Autowired
    private IPlayerService playerService;

    /**
     * 查询播放器配置列表
     */
    @PreAuthorize("@ss.hasPermi('basic:player:list')")
    @GetMapping("/list")
    public TableDataInfo list(Player player) {
        startPage();
        List<Player> list = playerService.selectPlayerList(player);
        return getDataTable(list);
    }

    /**
     * 查询启用的播放器配置列表(不分页)
     */
    @GetMapping("/activeList")
    public AjaxResult activeList() {
        List<Player> list = playerService.selectActivePlayerList();
        return success(list);
    }

    /**
     * 导出播放器配置列表
     */
    @PreAuthorize("@ss.hasPermi('basic:player:export')")
    @Log(title = "播放器配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Player player) {
        List<Player> list = playerService.selectPlayerList(player);
        ExcelUtil<Player> util = new ExcelUtil<Player>(Player.class);
        util.exportExcel(response, list, "播放器配置数据");
    }

    /**
     * 获取播放器配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('basic:player:query')")
    @GetMapping(value = "/{playerId}")
    public AjaxResult getInfo(@PathVariable("playerId") Long playerId) {
        return success(playerService.selectPlayerById(playerId));
    }

    /**
     * 新增播放器配置
     */
    @PreAuthorize("@ss.hasPermi('basic:player:add')")
    @Log(title = "播放器配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Player player) {
        if ("1".equals(playerService.checkCodeUnique(player))) {
            return error("新增播放器'" + player.getName() + "'失败,播放器标识已存在");
        }
        return toAjax(playerService.insertPlayer(player));
    }

    /**
     * 修改播放器配置
     */
    @PreAuthorize("@ss.hasPermi('basic:player:edit')")
    @Log(title = "播放器配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Player player) {
        if ("1".equals(playerService.checkCodeUnique(player))) {
            return error("修改播放器'" + player.getName() + "'失败,播放器标识已存在");
        }
        return toAjax(playerService.updatePlayer(player));
    }

    /**
     * 删除播放器配置
     */
    @PreAuthorize("@ss.hasPermi('basic:player:remove')")
    @Log(title = "播放器配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{playerIds}")
    public AjaxResult remove(@PathVariable Long[] playerIds) {
        return toAjax(playerService.deletePlayerByIds(playerIds));
    }

    /**
     * 校验播放器标识
     */
    @GetMapping("/checkCodeUnique")
    public String checkCodeUnique(Player player) {
        return playerService.checkCodeUnique(player);
    }

    /**
     * 导入播放器配置数据
     */
    @Log(title = "播放器配置", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('basic:player:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Player> util = new ExcelUtil<Player>(Player.class);
        List<Player> playerList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = playerService.importPlayer(playerList, updateSupport, operName);
        return success(message);
    }

    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Player> util = new ExcelUtil<Player>(Player.class);
        util.importTemplateExcel(response, "播放器配置数据");
    }
}
